package com.hzqy.service.activity.load;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hzqy.persistence.dao.activity.ActivityAnswerDao;
import com.hzqy.service.activity.interf.ActivityCountAbstract;
import com.hzqy.service.activity.interf.ActivityUserFactory;
import com.hzqy.web.vo.activity.AcContentActionVo;
import com.hzqy.web.vo.activity.AnswerUserVo;
import com.hzqy.web.vo.activity.ResultInfoVo;

public class ActivityAnswerUser implements ActivityUserFactory {
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	
	private ActivityCountAbstract activityCount;
	private ActivityAnswerDao activityAnswerDao;// = new ActivityAnswerDao();//ActivityAnswerDao.getInstance();
	public ActivityAnswerUser(){}
	public ActivityAnswerUser(ActivityCountAbstract activityCount){
		this.activityCount = activityCount;
	}
	public ActivityAnswerUser (ActivityAnswerDao activityAnswerDao){
		this.activityAnswerDao=activityAnswerDao;
	}
	public String getActivityUserPhone(Map map) {
		return activityAnswerDao.getAnswerPhone(map);
	}

	public int getActivityUserCount(Map map) {
		map = activityCount.getActivityCount(map);
		if(activityCount instanceof DayActivityCount){
			return activityAnswerDao.getAnswerCountDay(map);
		}else if(activityCount instanceof WeekActivityCount){
			return activityAnswerDao.getAnswerCountWeek(map);
		}else{
			return 999999;
		}
	}

	public List activityUserList(Map map) {
		return activityAnswerDao.luckyAnswerUserList(map);
	}

	public int insertActivityUserData(Map map) {
		AnswerUserVo answerVo = new AnswerUserVo();
		ResultInfoVo resultInfoVo = null;
		if(map.get("ResultInfoVo") != null){
			resultInfoVo = (ResultInfoVo) map.get("ResultInfoVo");
			answerVo.setF_aqu_info(resultInfoVo.getRetMsg());
		}else{
			answerVo.setF_aqu_info((String)map.get("detail"));
		}
		answerVo.setF_aqu_activityid(Integer.parseInt((String)map.get("activityid")));
		if(map.get("count") == null){
			if(resultInfoVo != null && "order".equals(resultInfoVo.getRetCode())){
				answerVo.setF_aqu_count(-1);//订购成功后送奖品
			}else{
				answerVo.setF_aqu_count(0);
			}
		}else{
			try {
				String count = (String) map.get("count");
				answerVo.setF_aqu_count(Integer.parseInt(count));
			} catch (Exception e) {
				answerVo.setF_aqu_count(-2);
				LOG.error("答题总数转换异常:",map.get("count")+";"+e);
			}
			
		}
		if(map.get("phone") == null){
			answerVo.setF_aqu_phone("");
		}else{
			answerVo.setF_aqu_phone((String)map.get("phone"));
		}
		answerVo.setF_aqu_serverid((String)map.get("serverid"));
		answerVo.setF_aqu_obligate1((String)map.get("f_aqu_obligate1"));
		answerVo.setF_aqu_obligate2((String)map.get("f_aqu_obligate2"));
		
		return activityAnswerDao.insertAnswerUser(answerVo);
	}

	public int updateActivityUserData(Map map) {
		int result = 0;
			result = activityAnswerDao.updateAnswerUser(map);
		if(result == 1){
			LOG.debug("修改答题记录成功");
		}else if(result == 0){
			LOG.debug("修改答题记录失败");
		}
		return result;
	}
	
	public int getAwardActivityUserCount(Map map) {
		map = activityCount.getActivityCount(map);
		if(activityCount instanceof DayActivityCount){
			return activityAnswerDao.getAwardAnswerCountDay(map);
		}else if(activityCount instanceof WeekActivityCount){
			return activityAnswerDao.getAwardAnswerCountWeek(map);
		}else{
			return 0;
		}
	}
	public void setActivityCount(ActivityCountAbstract activityCount) {
		this.activityCount = activityCount;
	}
	
}
